package com.resou.reader.reader.v;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.kyleduo.switchbutton.SwitchButton;
import com.resou.reader.R;
import com.resou.reader.api.entry.LoginData;
import com.resou.reader.api.entry.LoginResult;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.mine.listener.LoginListener;
import com.resou.reader.mine.login.LoginFragment;
import com.resou.reader.reader.p.ReadSetContract;
import com.resou.reader.reader.p.ReadSetPresenter;
import com.resou.reader.utils.ErrorUtils;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.PreferenceUtil;
import com.resou.reader.utils.ToastUtil;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReadSetActivity extends ResouBaseActivity<ReadSetPresenter> implements ReadSetContract.View {
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.sbAutoPay)
    SwitchButton sbAutoPay;
    @BindView(R.id.sbVolume)
    SwitchButton sbVolume;
    private String bookId;
    private boolean isCheck;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new ReadSetPresenter(this);
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        bookId = intent.getStringExtra("bookId");
        tvTitle.setText("阅读设置");
        if (UserInstance.isLogin())
            mPresenter.checkAutoBuy(UserInstance.getToken(), bookId);
        sbAutoPay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (UserInstance.isLogin())
                    isCheck = isChecked;
                mPresenter.setAutoBuy(UserInstance.getToken(), bookId, isChecked ? "open" : "close");
            }
        });
        sbAutoPay.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_UP == event.getAction()) {
                    if (!isNetwork()) {
                        sbAutoPay.setCheckedNoEvent(false);
                        return true;
                    } else {
                        return false;
                    }
                }
                return false;
            }
        });
        sbVolume.setChecked(PreferenceUtil.getInstance(ReadSetActivity.this).getVolumeState());
        sbVolume.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                PreferenceUtil.getInstance(ReadSetActivity.this).saveVolume(isChecked);
            }
        });
    }

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onSuccess(LoginResult<LoginData> result) {
//            Loading.getInstance().dismiss();
            LoginFragment.getInstance().dismiss();
            mPresenter.checkAutoBuy(UserInstance.getToken(), bookId);
            sbAutoPay.setCheckedNoEvent(true);
        }

        @Override
        public void onError(Throwable e) {
            ErrorUtils.showError(e);
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_read_set;
    }

    @Override
    public void retryRefreshData() {

    }

    @Override
    public <T> void onSuccess(T t, int type) {
        switch (type) {
            case 0:
                boolean isAutoBuy = (Boolean) t;
                sbAutoPay.setChecked(isAutoBuy);
                break;
            case 1:
                sbAutoPay.setChecked(isCheck);
                Intent intent = new Intent();
                intent.putExtra("isCheck", isCheck);
                setResult(Activity.RESULT_OK, intent);
                break;
        }

    }

    @Override
    public void initData() {

    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showError(Throwable throwable) {

    }

    @Override
    public void showEmpty() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ivBack, R.id.sbAutoPay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
